package world3d;

import java.util.ArrayList;

import javax.media.j3d.BranchGroup;
import javax.vecmath.Point3d;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3f;

public class TexturedFloor{
	
	private final static String ROAD = "textures/road_dirt.jpg";
	private final static String GRASS = "textures/grass_2.jpg";
	private final static String ROAD_INTERSECT = "textures/road_dirt_intersect.jpg";
	
	private BranchGroup floorBg;
	
	// temporary solution
	private Point3d[] inter;
	
	public TexturedFloor(){
		inter = new Point3d[2];
		ArrayList<Point3f> roadCoords = new ArrayList<Point3f>();
		ArrayList<Point3f> grassCoords = new ArrayList<Point3f>();
		ArrayList<Point3f> intersectedCoords = new ArrayList<Point3f>();
		ArrayList<Point3f> intersectCoords = new ArrayList<Point3f>();
		
		BranchGroup bg = new BranchGroup();
		Vector3f normal = new Vector3f(0.0f, 1.0f, 0.0f);
		
		int roadStarts = (World3dConstants.WORLD_SIZE - World3dConstants.ROAD_LENGTH) / 2;
		
		for(int i = 0; i <= World3dConstants.WORLD_SIZE; i += World3dConstants.CELL_SIZE){
			for(int j = 0; j <= World3dConstants.WORLD_SIZE; j += World3dConstants.CELL_SIZE){
				
				if(j >= roadStarts && j < (roadStarts + World3dConstants.ROAD_LENGTH) && i == roadStarts){
					createCoords(i, j, roadCoords);
				}else if(j == (roadStarts + World3dConstants.ROAD_LENGTH)){
					if(i == roadStarts){
						createCoords(i, j, intersectCoords);
						// inter is the bounding box used to stop the user when
						// he's approaching the intersection
						inter[0] = new Point3d(intersectCoords.get(1));
						inter[1] = new Point3d(intersectCoords.get(3));
						inter[0].z -= 3.0;
						inter[1].z -= 3.0;
						inter[1].y -= 10.0;
						inter[0].y += 10.0;
					}else{
						createCoords(i, j, intersectedCoords);
					}
				}else{
					createCoords(i, j, grassCoords);
				}
			}
		}
		
		System.out.println("Grass coords : " + grassCoords.size());
		System.out.println("Road coords : " + roadCoords.size());
		System.out.println("Intersected coords : " + intersectedCoords.size());
		System.out.println("Intersection coords : " + intersectCoords.size());
		
		bg.addChild(new TexturedPlane(grassCoords, normal, GRASS, TexturedPlane.BOTTOM));
		bg.addChild(new TexturedPlane(roadCoords, normal, ROAD, TexturedPlane.BOTTOM));
		bg.addChild(new TexturedPlane(intersectedCoords, normal, ROAD, TexturedPlane.TOP));
		bg.addChild(new TexturedPlane(intersectCoords, normal, ROAD_INTERSECT, TexturedPlane.BOTTOM));
		this.setFloor(bg);
	}
	
	public Point3d[] getIntersection(){
		return inter;
	}
	
	private void createCoords(int x, int z, ArrayList<Point3f> coords){
		int step = World3dConstants.CELL_SIZE;
		Point3f p1 = new Point3f(x - step, 0, z);
		Point3f p2 = new Point3f(x, 0, z);
		Point3f p3 = new Point3f(x, 0, z - step);
		Point3f p4 = new Point3f(x - step, 0, z - step);
		
		coords.add(p1);
		coords.add(p2);
		coords.add(p3);
		coords.add(p4);
	}
	
	protected void setFloor(BranchGroup bg){
		this.floorBg = bg;
	}
	
	public BranchGroup getFloor(){
		return floorBg;
	}
}
